package GUI;


import Control.User;
import Control.Privilege;
import Control.ImportExport;
import Control.Database;
import java.awt.BorderLayout;
import java.awt.Color;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author dodan_000
 */
public class StatisticsChartFrame extends javax.swing.JFrame {

    /**
     * Creates new form StatisticsChartFrame
     */
    ChartFrame chartFrame;
    JFreeChart chart;
    DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();;
    DefaultPieDataset pieDataset = new DefaultPieDataset();
    ChartPanel chartPanel = new ChartPanel(chart); 
    User user = new  User();
    ImportExport importExport = new ImportExport();
    Privilege privilege = new Privilege();
    
    public StatisticsChartFrame(String userName) {
        user.userName = userName;
        user.id = user.getUserId(userName);
        initComponents();
        initChartFrame();

    }
    
    public StatisticsChartFrame() {
        initComponents();
        initChartFrame();

    }
    
    private void initChartFrame(){
        //test
        Database.setState();
        user.userName = "hieu";
        user.id = 2;
        //end test
        ArrayList<String> list = new ArrayList<>();
        
        list = user.getUserList();
        for(String s:list){
            cboxChartUser.addItem(s);
        }
        cboxChartUser.setSelectedItem(user.userName);
        
        list = importExport.getImportTypeList();
        for(String s:list){
            cboxChartSubType.addItem(s);
        }
        
        chartPanel.setChart(chart);
        jpChart.setLayout(new BorderLayout());
        jpChart.add(chartPanel);      

    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jpChart = new javax.swing.JPanel();
        jpChart1 = new javax.swing.JPanel();
        cboxChartUser = new javax.swing.JComboBox();
        jCheckBoxMonth = new javax.swing.JCheckBox();
        jCheckBoxSubType = new javax.swing.JCheckBox();
        jCheckBoxYear = new javax.swing.JCheckBox();
        jycChart = new com.toedter.calendar.JYearChooser();
        jmcChart = new com.toedter.calendar.JMonthChooser();
        cboxChartType = new javax.swing.JComboBox();
        cboxChartSubType = new javax.swing.JComboBox();
        btnChartView = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(1024, 590));
        setPreferredSize(new java.awt.Dimension(1024, 590));
        setResizable(false);

        jLayeredPane1.setPreferredSize(new java.awt.Dimension(1024, 590));

        jpChart.setBackground(new java.awt.Color(150, 210, 222));
        jpChart.setMinimumSize(new java.awt.Dimension(795, 576));
        jpChart.setPreferredSize(new java.awt.Dimension(795, 540));

        javax.swing.GroupLayout jpChartLayout = new javax.swing.GroupLayout(jpChart);
        jpChart.setLayout(jpChartLayout);
        jpChartLayout.setHorizontalGroup(
            jpChartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jpChartLayout.setVerticalGroup(
            jpChartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jpChart1.setBackground(new java.awt.Color(150, 210, 222));
        jpChart1.setMinimumSize(new java.awt.Dimension(795, 540));
        jpChart1.setPreferredSize(new java.awt.Dimension(225, 576));

        jCheckBoxMonth.setText("Theo tháng");

        jCheckBoxSubType.setText("Theo loại");

        jCheckBoxYear.setText("Theo năm");

        jmcChart.setYearChooser(jycChart);

        cboxChartType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Import", "Export" }));
        cboxChartType.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboxChartTypeItemStateChanged(evt);
            }
        });

        cboxChartSubType.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cboxChartSubTypeMouseClicked(evt);
            }
        });
        cboxChartSubType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboxChartSubTypeActionPerformed(evt);
            }
        });

        btnChartView.setText("View");
        btnChartView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChartViewActionPerformed(evt);
            }
        });

        jLabel1.setText("BarChart");

        jCheckBox1.setText("Bar Chart");
        jCheckBox1.setRolloverEnabled(false);

        javax.swing.GroupLayout jpChart1Layout = new javax.swing.GroupLayout(jpChart1);
        jpChart1.setLayout(jpChart1Layout);
        jpChart1Layout.setHorizontalGroup(
            jpChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpChart1Layout.createSequentialGroup()
                .addGroup(jpChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpChart1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jpChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jmcChart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jycChart, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jpChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBoxMonth)
                            .addComponent(jCheckBoxYear, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jpChart1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jpChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jpChart1Layout.createSequentialGroup()
                                .addComponent(cboxChartUser, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jCheckBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jpChart1Layout.createSequentialGroup()
                                .addGroup(jpChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cboxChartSubType, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cboxChartType, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jpChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jCheckBoxSubType, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jpChart1Layout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addComponent(btnChartView, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpChart1Layout.setVerticalGroup(
            jpChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpChart1Layout.createSequentialGroup()
                .addGap(82, 82, 82)
                .addGroup(jpChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox1)
                    .addComponent(cboxChartUser, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jpChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboxChartType, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jpChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboxChartSubType, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBoxSubType))
                .addGap(11, 11, 11)
                .addGroup(jpChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jmcChart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBoxMonth))
                .addGap(6, 6, 6)
                .addGroup(jpChart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jycChart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBoxYear))
                .addGap(53, 53, 53)
                .addComponent(btnChartView)
                .addContainerGap(246, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jLayeredPane1Layout = new javax.swing.GroupLayout(jLayeredPane1);
        jLayeredPane1.setLayout(jLayeredPane1Layout);
        jLayeredPane1Layout.setHorizontalGroup(
            jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jLayeredPane1Layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addComponent(jpChart1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jpChart, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jLayeredPane1Layout.setVerticalGroup(
            jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jLayeredPane1Layout.createSequentialGroup()
                .addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpChart1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jpChart, javax.swing.GroupLayout.PREFERRED_SIZE, 532, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 14, Short.MAX_VALUE))
        );
        jLayeredPane1.setLayer(jpChart, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jLayeredPane1.setLayer(jpChart1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap(157, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnChartViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChartViewActionPerformed
        

        int userId = user.getUserId((String) cboxChartUser.getSelectedItem());
        int year = jycChart.getYear();
        int month = jmcChart.getMonth() + 1;
        int subTypeId = importExport.getImportId((String) cboxChartSubType.getSelectedItem());
        int caseSQL = (jCheckBoxSubType.isSelected() ? 4 : 0) + (jCheckBoxMonth.isSelected() ? 2 : 0) + (jCheckBoxYear.isSelected() ? 1 : 0);
       
        chart = importExport.getChart(userId, rootPaneCheckingEnabled, subTypeId, month, year, caseSQL, jCheckBox1.isSelected()?1:2);
        chartPanel.setChart(chart);
        
    }//GEN-LAST:event_btnChartViewActionPerformed

    private void cboxChartTypeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboxChartTypeItemStateChanged
        // TODO add your handling code here:
        ArrayList<String> listSubType = new ArrayList<>();
        cboxChartSubType.removeAllItems();
        if(cboxChartType.getSelectedIndex()==0){
            listSubType = importExport.getImportTypeList();
            
            for(String s: listSubType){
                cboxChartSubType.addItem(s);
            }
        }
        else{
            listSubType = importExport.getExportTypeList();
            for(String s: listSubType){
                cboxChartSubType.addItem(s);
            }
        }
    }//GEN-LAST:event_cboxChartTypeItemStateChanged

    private void cboxChartSubTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboxChartSubTypeActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_cboxChartSubTypeActionPerformed

    private void cboxChartSubTypeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cboxChartSubTypeMouseClicked
        // TODO add your handling code here:
        System.out.println("ban vua click vao cbox Type");
    }//GEN-LAST:event_cboxChartSubTypeMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StatisticsChartFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StatisticsChartFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StatisticsChartFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StatisticsChartFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StatisticsChartFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChartView;
    private javax.swing.JComboBox cboxChartSubType;
    private javax.swing.JComboBox cboxChartType;
    private javax.swing.JComboBox cboxChartUser;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBoxMonth;
    private javax.swing.JCheckBox jCheckBoxSubType;
    private javax.swing.JCheckBox jCheckBoxYear;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private com.toedter.calendar.JMonthChooser jmcChart;
    private javax.swing.JPanel jpChart;
    private javax.swing.JPanel jpChart1;
    private com.toedter.calendar.JYearChooser jycChart;
    // End of variables declaration//GEN-END:variables
}
